using System;
using System.Collections.Generic;
using System.Reflection;


namespace Framework.Network
{
    public sealed partial class NetworkManager : FrameworkModule
    {
        private readonly Dictionary<ushort, Type> m_Opcode2Type = new Dictionary<ushort, Type>();
        private readonly Dictionary<Type, ushort> m_Type2Opcode = new Dictionary<Type, ushort>();

        public void InitOpcodeType()
        {
            foreach (Type type in FrameworkEntry.GetTypes(typeof(MessageAttribute)))
            {
                MessageAttribute attr = (MessageAttribute)type.GetCustomAttribute(typeof(MessageAttribute));
                if (attr != null)
                {
                    this.m_Opcode2Type.Add(attr.Opcode, type);
                    this.m_Type2Opcode.Add(type, attr.Opcode);
                }
            }
        }

        public object GetMessage(ushort opcode)
        {
            Type type = this.m_Opcode2Type[opcode];
            return Activator.CreateInstance(type);
        }

        public ushort GetOpcode(Type type)
        {
            return this.m_Type2Opcode[type];
        }
    }
}